package com.shareach.basic;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import com.shareach.util.JsonUtil;
import com.shareach.web.basic.Answer;
import com.shareach.web.basic.Ask;
import com.shareach.web.basic.IAnswerData;


public class MongoUtilDao{
	private static String HttpMessageCollectionName = "HttpMessage";
	
	private MongoTemplate mongoTemplate;
	
	public MongoUtilDao(MongoTemplate mongoTemplate){
		this.mongoTemplate = mongoTemplate;
	}
	@Async
	public void recordRequestLog(HashMap<String, Object> data, Object ask , Object answer){
		data.put("cmd", 0);
		data.put("time", new Date());
		String s = JsonUtil.jsonFromObject(ask);
		data.put("request", JSON.parse(s));
		s = JsonUtil.jsonFromObject(answer);
		data.put("response", JSON.parse(s));
	}
	/**
	 * 保存访问日志
	 * @param ask
	 * @param answer
	 * @throws Exception
	 */
	public void recordRequestLog(String ip, Ask ask, Answer<?> answer) throws Exception {
		DBObject object = new BasicDBObject();
		object.put("cmd", ask.getCmd());
		object.put("time", new Date());
		object.put("ip", ip);
		object.put("sn", ask.getSn());
		DBObject parameter = new BasicDBObject();
		Map<String,Object> map = ask.getRequest();
		if(map!=null){
			parameter.putAll(ask.getRequest());
		}
	    object.put("request", parameter);
		DBObject result = new BasicDBObject();
		result.put("code", answer.getCode());
		Object rs = answer.getResult();
		if(rs!=null){
			if(rs instanceof IAnswerData){
				 result.put("result", ((IAnswerData)rs).decodeMongo());
			}else if(rs instanceof Object){
				if (rs instanceof Integer || 
					rs instanceof String ||
					rs instanceof Long ||
					rs instanceof Date ||
					rs instanceof Number ||
					rs instanceof Short ||
					rs instanceof Double ||
					rs instanceof Float){
					result.put("result", rs.toString());
				}else{
					String s = JsonUtil.jsonFromObject(rs);
					//String s = JSON.serialize(rs);
					result.put("result", JSON.parse(s));
				}
			}else{
				result.put("result", rs.toString());
			}
		}
		object.put("response", result);
		mongoTemplate.save(object,HttpMessageCollectionName);
	}

	/**
	 * 保存数据对象
	 * @param data
	 * @param id
	 * @param collectionName
	 */
	public void SaveObject(Object data,Object id, String collectionName){
		String s = JsonUtil.jsonFromObject(data);
		Object obj = JSON.parse(s);
		DBObject result = new BasicDBObject();
		result.put("data", obj);
		result.put("_id", id);
		result.put("time", new Date());
		mongoTemplate.save(result,collectionName);
	}
	
	/**
	 * 获取指定Id的对象
	 * @param poiid
	 * @return
	 */
	public Object getObject(Object id, String collectionName){
		DBObject query =new BasicDBObject("_id",id);
		DBObject result = mongoTemplate.getCollection(collectionName).findOne(query);
		if(result!=null && result.containsField("data")){
			return result.get("data");
		}else{
			return null;
		}
	}
}
